package com.example.demo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.demo.entity.MenuEntity;
import com.example.demo.entity.base.TreeEntity;

/**
 * 
 * @author xzw
 *
 */
public class TreeUtil {
	/**
	 * 
	 * @方法名 TreeUtil.java
	 * @创建者 xzw
	 * @创建日期 2021年7月2日
	 * @返回类型 List<MenuEntity>
	 * @功能描述 组装菜单树结构
	 * 
	 */
	public static List<MenuEntity> installMenuTree(List<MenuEntity> menuList) throws Exception{
		List<MenuEntity> treeMenu = new ArrayList<>(menuList);
		
		for (MenuEntity menuEntity : menuList) {
			for (MenuEntity menuEntity2 : menuList) {
				if(menuEntity.getMenu_id().equals(menuEntity2.getParent_id())) {
					if(null==menuEntity.getChilds()) {
						menuEntity.setChilds(new ArrayList<>());
					}
					menuEntity.getChilds().add(menuEntity2);
					treeMenu.remove(menuEntity2);
				}
			}
		}
		
		return treeMenu;
	}
	
	/**
	 * 
	 * @方法名 TreeUtil.java
	 * @创建者 xzw
	 * @创建日期 2021年7月2日
	 * @返回类型 List<Map<String,Object>>
	 * @功能描述 组装组织机构树
	 *
	 */
	public static List<Map<String, Object>> installOrgTree(List<Map<String, Object>> orgList) throws Exception{
		List<Map<String, Object>> treeOrg = new ArrayList<>(orgList);
		
		for (Map<String, Object> orgNode : orgList) {
			for (Map<String, Object> orgNode2 : orgList) {
				if(orgNode.get("id").equals(orgNode2.get("parentId"))) {
					if(null==orgNode.get("children")) {
						orgNode.put("children", new ArrayList<>());
					}
					List<Map<String, Object>> children = (List<Map<String, Object>>) orgNode.get("children");
					children.add(orgNode2);
					treeOrg.remove(orgNode2);
				}
			}
		}
		
		return treeOrg;
	}
	
	/**
	 * 
	 * @方法名 TreeUtil.java
	 * @创建者 xzw
	 * @创建日期 2021年7月2日
	 * @返回类型 List<TreeEntity>
	 * @功能描述 组装常用节点类树
	 *
	 */
	public static List<TreeEntity> installCommonTree(List<TreeEntity> treeList) throws Exception{
		List<TreeEntity> tree = new ArrayList<>(treeList);
		
		for (TreeEntity treeEntity : treeList) {
			for (TreeEntity treeEntity2 : treeList) {
				if(treeEntity.getId().equals(treeEntity2.getParentId())) {
					if(null==treeEntity.getChildren()) {
						treeEntity.setChildren(new ArrayList<>());
					}
					treeEntity.getChildren().add(treeEntity2);
					tree.remove(treeEntity2);
				}
			}
		}
		return tree;
	}
}
